package com.servlet;

import com.entity.Foods;
import com.service.impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/comment.let")
public class CommentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String type = req.getParameter("type");
        CommentServiceImpl commentService = new CommentServiceImpl();
        switch (type){
            case "queryAllComment":
                int foodId= Integer.parseInt(req.getParameter("foodId"));
                int typeId= Integer.parseInt(req.getParameter("typeId"));
                int pageIndex=Integer.parseInt(req.getParameter("pageIndex"));
                int pageSize=5;
                int pageCount = commentService.getPageCount(pageSize,foodId);
                if(pageIndex<1){
                    pageIndex=1;
                }
                if(pageIndex>pageCount){
                    pageIndex=pageCount;
                }
                List<Map<String,Object>> comments=commentService.queryAllComment(foodId,pageIndex,pageSize);
                if (comments!=null){
                    session.setAttribute("comments",comments);
                    req.setAttribute("pageCount",pageCount);
                    req.getRequestDispatcher("single-product.jsp?foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex).forward(req,resp);
                }else {
                    req.getRequestDispatcher("single-product.jsp").forward(req,resp);
                }

                break;
            case  "addComment":
                int userId=Integer.parseInt(req.getParameter("userID"));
                String comment=req.getParameter("comment");
                foodId=Integer.parseInt(req.getParameter("foodID"));
                typeId=Integer.parseInt(req.getParameter("typeID"));
                pageIndex=Integer.parseInt(req.getParameter("pageIndex"));
                int addComment=commentService.addCommentByUserID(userId,comment,foodId);
                if (addComment!=0){
                    out.println("<script>location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"';</script>");
                }else {
                    out.println("<script>alert('评论失败');parent.window.location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"'';</script>");
                }
                break;
            case "deleteComment":
                int commentId=Integer.parseInt(req.getParameter("commentId"));
                foodId=Integer.parseInt(req.getParameter("foodId"));
                typeId=Integer.parseInt(req.getParameter("typeId"));
                pageIndex=Integer.parseInt(req.getParameter("pageIndex"));
                int count=commentService.deleteCommentById(commentId);
                if (count!=0){
                    out.println("<script>location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"';</script>");
                }else {
                    out.println("<script>alert('删除评论失败');parent.window.location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"'';</script>");
                }
            case "modifyComment":
                commentId=Integer.parseInt(req.getParameter("commentId"));
                comment=req.getParameter("comment");
                foodId=Integer.parseInt(req.getParameter("foodID"));
                typeId=Integer.parseInt(req.getParameter("typeID"));
                pageIndex=Integer.parseInt(req.getParameter("pageIndex"));
                count=commentService.modifyComment(commentId,comment);
                if (count!=0){
                    out.println("<script>location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"';</script>");
                }else {
                    out.println("<script>alert('修改评论失败');parent.window.location.href='/foods.let?type=findId&foodId="+foodId+"&typeId="+typeId+"&pageIndex="+pageIndex+"'';</script>");
                }
        }
    }
}
